package com.cet.regularexpression;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: algorithm
 * @description: Hello, ${name}! You are learning ${lang}!
 * @author: 陈恩涛
 * @create: 2021-06-22 15:25
 **/
public class Main14 {

    private static Pattern MY_PATTERN = Pattern.compile("\\$\\{(\\w+)\\}");

    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<>(16);
        map.put("name","chenentao");
        map.put("lang","regularexpression");

        StringBuilder s = new StringBuilder();
        Matcher matcher = MY_PATTERN.matcher("Hello, ${name}! You are learning ${lang}!");
        while (matcher.find()) {
           matcher.appendReplacement(s,map.get(matcher.group(1)));
        }
        System.out.println(s.toString());
        matcher.appendTail(s);
        System.out.println(s.toString());
    }
}
